package com.example.controller.user;



import com.example.common.result.Result;
import com.example.pojo.dto.UserDTO;
import com.example.pojo.dto.UserLoginDTO;
import com.example.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.DataOutputStream;
import java.io.IOException;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
//    @GetMapping
//    public void register(String name, String password, HttpServletResponse response) throws IOException {
//        System.out.println(name);
//        System.out.println(password);
//        boolean flag=userService.addUser(name,password);
//        String Result="T";
//        if (!flag){
//            Result="F";
//            log.info("注册失败");
//        }
//        log.info("注册用户名：{}",name);
//        DataOutputStream dos=new DataOutputStream(response.getOutputStream());
//        dos.writeUTF(Result);
//    }

    @Operation(summary = "用户注册", description = "用户注册")
    @ApiResponses(
            @ApiResponse(responseCode = "200", description = "注册成功")
    )
    @PostMapping("/register")
    //TODO 添加jwt令牌
    public Result register(@RequestBody UserDTO userDTO) throws IOException {
        log.info("注册用户：{}",userDTO);
        boolean flag=userService.addUser(userDTO);
//        DataOutputStream dos=new DataOutputStream(response.getOutputStream());
//        dos.writeUTF(Result);
        return Result.success();
    }
    @Operation(summary = "用户登录", description = "用户登录")
    @ApiResponses(
            @ApiResponse(responseCode = "200", description = "登录成功")
    )
    @PostMapping("/login")
    public Result login(@RequestBody UserLoginDTO userLoginDTO) throws IOException {
        log.info("用户登录：{}",userLoginDTO);
        userService.login(userLoginDTO);
        return Result.success();
    }
}
